// pages/mine_center/order_detail.js
const app = getApp();
var request = require("../../../utils/request.js");
var wx_alert = require("../../../utils/wx_alert.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_true:true,
    reasons_list:[
      {
        title:"不喜欢、不想要",
        select:"true"
      },
      {
        title: "不喜欢、不想要",
        select: false
      },
      {
        title: "不喜欢、不想要",
        select: "true"
      },
      {
        title: "其他原因",
        select: false
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.turn = request.turn;
     var that = this;
     this.setData({
       options: options
     })
    var getStore_data = {
      limit: 2,
      page: 1,
      city: "南京",
      keyword: "",
      classifyId: "0",
      sort: "1",
      brandId: "0"
    }
    request.request(app.globalData.getStoreList, getStore_data, function (res) {
      console.log(res)
      if (res.code == 0) {
        for (var j in res.storeExlist) {
          var label = res.storeExlist[j].label;
          res.storeExlist[j].label = label.split(",");
        }
        that.setData({
          order_storelist: res.storeExlist
        })
      }
    })     
    
    request.request(app.globalData.payForOrder, { orderId: options.order_id, mobileType: "phone" }, function (res) {
      console.log(res)
      if (res.code == 0) {
         that.setData({
           params: res.params
         })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var data = {
      orderId: this.data.options.order_id,
    }
    var that = this;
    request.request(app.globalData.getOrderDetail, data, function (res) {
      console.log(res)
      if (res.code == 0) {
        var order_lists = [];
        var total_price = 0;
        var sn = {
          price: res.orderDetail.snPrice,
          title: res.orderDetail.snName,
          num: res.orderDetail.num,
          snId: res.orderDetail.id
        }
        order_lists.push(sn)
        var upgradeInfo = JSON.parse(res.orderDetail.upgradeInfo);
        for (var i in upgradeInfo) {
          order_lists.push(upgradeInfo[i])
        }

        for (var j in order_lists) {
          total_price += Number(order_lists[j].num * order_lists[j].price);
        }

        that.setData({
          orderDetail: res.orderDetail,
          order_lists: order_lists,
          total_price: total_price,
          order_id: that.data.options.order_id,
          img_url: request.url + "/",
          productId: res.orderDetail.productId,
        })


        request.request(app.globalData.getReasonList, { type: res.orderDetail.state }, function (res_data) {
          console.log(res_data)
          if (res_data.code == 0) {
            that.setData({
              reasons_list: res_data.list
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  back_index:function(){
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  back_money:function(){
    var order_true = this.data.order_true; 
    order_true = !order_true;
    this.setData({
      order_true: order_true
    })
  },
  back_order:function(){

    var that = this;
    var order_true = this.data.order_true;
    var orderDetail = this.data.orderDetail;
    order_true = !order_true;
    var state;
    if (orderDetail.state == 1) {
      state = -1
    }
    if (orderDetail.state == 2) {
      state = 5
    }
    request.request(app.globalData.updateOrder, { orderId: orderDetail.id, type: state, reason: that.data.select_content }, function (res) {

      if (res.code == 0) {
        that.setData({
          order_true: order_true
        })
        if (state == -1) {
          var content = "取消成功"
        }
        if (state == 5) {
          var content = "退款成功"
        }
        wx_alert.alert(content, function () {
          setTimeout(function () {
            that.onShow();
          }, 400)
        })
      }

    })
    
  },
  select_reason:function(e){
    var index = e.currentTarget.dataset.index;
    this.setData({
      select_index:index,
      select_content: this.data.reasons_list[index].content
    })
  },
  // 加
  plus: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_lists = this.data.order_lists;
    order_lists[index].num++;
    var data;
    if (order_lists[index].snId) {
      data = {
        orderId: this.data.order_id,
        productId: that.data.productId,
        goodNum: order_lists[index].num,
      }
    } else {
      data = {
        orderId: this.data.order_id,
        productId: that.data.productId,
        upNum: order_lists[index].num,
        upId: order_lists[index].id
      }
    }

    request.request(app.globalData.changeOrderNum, data, function (res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          order_lists: order_lists
        })
        num(index, that, "plus");
      }
    })


  },
  // 减
  pluase: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_lists = this.data.order_lists;
    if (order_lists[index].num == 1) {
      wx_alert.confirm("确定删除此商品吗？", function () {
        num(index, that, "pluase");
        order_lists.splice(index, 1);
        that.setData({
          order_lists: order_lists
        })
      })
    } else {
      order_lists[index].num--;

      var data;
      if (order_lists[index].snId) {
        data = {
          orderId: this.data.order_id,
          productId: that.data.productId,
          goodNum: order_lists[index].num,
        }
      } else {
        data = {
          orderId: this.data.order_id,
          productId: that.data.productId,
          upNum: order_lists[index].num,
          upId: order_lists[index].id
        }
      }
      request.request(app.globalData.changeOrderNum, data, function (res) {
        console.log(res)
        if (res.code == 0) {
          that.setData({
            order_lists: order_lists
          })
          num(index, that, "pluase");
        }
      })

    }
  },  
  call_phone: function (e) {
    console.log(e)
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone //仅为示例，并非真实的电话号码
    })
  },    
  now_pay: function (e) {
      console.log(e)
        wx.request({
          url: "https://www.99bill.com/mobilegateway/recvMerchanInfoAction.htm",
          data: e.detail.value,
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          method: "POST",
          success: function (res) {
            console.log(res)

          }
        })
  },  
  tap_product_detail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/phone_detail/phone_detail?productId=' + id,
    })
  },  

})

function num(index, that, status) {
  var order_lists = that.data.order_lists;
  var total_price = Number(that.data.total_price);
  var price = order_lists[index].price;
  if (status == "plus") {
    total_price += Number(price);
  } else {
    total_price -= Number(price);
  }

  that.setData({
    total_price: total_price
  })
}